package com.fzq.service.impl;

import com.fzq.service.MaskingStrategy;
import org.springframework.stereotype.Component;

@Component
public class NameMaskingStrategy implements MaskingStrategy {


    /**
     * 姓名数据脱敏
     * @param value
     * @param pattern
     * @return
     */
    @Override
    public String mask(String value, String pattern) {
        if (value == null || value.length() <= 1) {
            return value;
        }
        if (value.length() == 2) {
            return value.charAt(0) + "*";
        }

        // 替代 String.repeat() 的兼容写法
        int repeatCount = value.length() - 2;
        StringBuilder stars = new StringBuilder();
        for (int i = 0; i < repeatCount; i++) {
            stars.append('*');
        }

        return value.charAt(0) + stars.toString() + value.charAt(value.length() - 1);
    }
}
